﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Course_UserControls_AssessmentQuestion : System.Web.UI.UserControl
{
    private int _AssessmentItemID;
    private string _Title;
    private string _QuestionText;
    private AssessmentItem _Data;
    private AssessmentItem.ItemTypes _QuestionType;

    public int AssessmentItemID
    {
        get
        {
            return _AssessmentItemID;
        }
        set
        {
            _AssessmentItemID = value;
        }
    }

    public string Title
    {
        get
        {
            return _Title;
        }
        set
        {
            _Title = value;
        }
    }

    public string QuestionText
    {
        get
        {
            return _QuestionText;
        }
        set
        {
            _QuestionText = value;
        }
    }

    public AssessmentItem Data
    {
        get
        {
            return _Data;
        }
        set
        {
            _Data = value;
        }
    }

    public AssessmentItem.ItemTypes QuestionType
    {
        get
        {
            return _QuestionType;
        }
        set
        {
            _QuestionType = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        RadioButtonList rbl;
        CheckBoxList cbl;
        TextBox tb;
        Label lb;

        _Title = _Data.ItemTitle;
        _QuestionText = _Data.ItemText;
        _QuestionType = _Data.ItemType;
        aImage.ImageUrl = _Data.ImageURL;

        if (_QuestionType == AssessmentItem.ItemTypes.MultipleChoiceSingleAnswer)
        {
            rbl = new RadioButtonList();
            rbl.DataTextField = "AnswerText";
            rbl.DataValueField = "Identifier";
            rbl.DataSource = _Data.Answers;
            rbl.SelectedIndexChanged += new EventHandler(rbl_SelectedIndexChanged);
            rbl.DataBind();
            if (_Data.AnswerGiven != "")
                rbl.Enabled = false;
            pnlAnswers.Controls.Add(rbl);
        }
        if (_QuestionType == AssessmentItem.ItemTypes.MultipleChiceMultipleAnswer)
        {
            cbl = new CheckBoxList();
            cbl.DataTextField = "AnswerText";
            cbl.DataValueField = "Identifier";
            cbl.DataSource = _Data.Answers;
            cbl.SelectedIndexChanged += new EventHandler(cbl_SelectedIndexChanged);
            cbl.DataBind();
            if (_Data.AnswerGiven != "")
                cbl.Enabled = false;
            pnlAnswers.Controls.Add(cbl);
        }
        if (_QuestionType == AssessmentItem.ItemTypes.Text)
        {
            tb = new TextBox();
            tb.TextChanged += new EventHandler(tb_TextChanged);
            if (_Data.AnswerGiven != "")
                tb.Text = _Data.AnswerGiven;
            pnlAnswers.Controls.Add(tb);
        }
        if (_QuestionType == AssessmentItem.ItemTypes.PatternMatch)
        {
            tb = new TextBox();
            tb.TextChanged += new EventHandler(tb_TextChanged);
            if (_Data.AnswerGiven != "")
                tb.Text = _Data.AnswerGiven;
            pnlAnswers.Controls.Add(tb);
        }
        if (_QuestionType == AssessmentItem.ItemTypes.Essay)
        {
            tb = new TextBox();
            tb.TextChanged += new EventHandler(tb_TextChanged);
            tb.TextMode = TextBoxMode.MultiLine;
            tb.Height = 100;
            tb.Width = 800;
            if (_Data.AnswerGiven != "")
                tb.Text = _Data.AnswerGiven;
            pnlAnswers.Controls.Add(tb);
        }

        if (_Data.AnswerGiven != "")
        {
            if (_QuestionType == AssessmentItem.ItemTypes.MultipleChiceMultipleAnswer || _QuestionType == AssessmentItem.ItemTypes.MultipleChoiceSingleAnswer)
            {
                pnlFeedback.Controls.Clear();
                pnlFeedback.BorderColor = System.Drawing.Color.Black;
                pnlFeedback.BorderStyle = BorderStyle.Solid;
                pnlFeedback.BorderWidth = 1;

                lb = new Label();
                lb.Text = _Data.AnswerGiven;
                lb.Font.Size = 12;
                pnlFeedback.Controls.Add(lb);
            }
        }
    }

    public void ShowSelectedAnswerFeedBack()
    {
        pnlFeedback.Controls.Clear();
        pnlFeedback.BorderColor = System.Drawing.Color.Black;
        pnlFeedback.BorderStyle = BorderStyle.Solid;
        pnlFeedback.BorderWidth = 1;
        bool FBGiven = false;

        foreach (AssessmentAnswer aa in _Data.Answers)
        {
            if (hidResponseID.Value == aa.Identifier)
            {
                foreach (AssessmentResponse rsp in aa.Responses)
                {
                    foreach (FeedBack fb in rsp.FeedBacks)
                    {
                        Label lbl = new Label();
                        if (rsp.Score == 100)
                            lbl.ForeColor = System.Drawing.Color.DarkGreen;
                        else if (rsp.Score == -1)
                            lbl.ForeColor = System.Drawing.Color.Black;
                        else if (rsp.Score == 0)
                            lbl.ForeColor = System.Drawing.Color.DarkRed;
                        lbl.Font.Size = 12;
                        lbl.Text = fb.FeedBackText + "<br><br>";
                        pnlFeedback.Controls.Add(lbl);
                        FBGiven = true;
                    }
                }
            }
            if (_Data.ItemType == AssessmentItem.ItemTypes.PatternMatch || _Data.ItemType == AssessmentItem.ItemTypes.Text)
            {
                foreach (AssessmentResponse rsp in aa.Responses)
                {
                    if (hidResponseID.Value == rsp.Pattern)
                    {
                        foreach (FeedBack fb in rsp.FeedBacks)
                        {
                            Label lbl = new Label();
                            if (rsp.Score == 100)
                                lbl.ForeColor = System.Drawing.Color.DarkGreen;
                            else if (rsp.Score == -1)
                                lbl.ForeColor = System.Drawing.Color.Black;
                            else if (rsp.Score == 0)
                                lbl.ForeColor = System.Drawing.Color.DarkRed;
                            lbl.Font.Size = 12;
                            lbl.Text = fb.FeedBackText + "<br><br>";
                            pnlFeedback.Controls.Add(lbl);
                            FBGiven = true;
                        }
                    }
                }
            }
        }

        if (!FBGiven)
            pnlFeedback.Visible = false;
        else
            pnlFeedback.Visible = true;
    }

    public AssessmentResponse GetResponse()
    {
        foreach (AssessmentAnswer aa in _Data.Answers)
        {
            if (_Data.ItemType == AssessmentItem.ItemTypes.MultipleChiceMultipleAnswer || Data.ItemType == AssessmentItem.ItemTypes.MultipleChoiceSingleAnswer)
            {
                if (hidResponseID.Value == aa.Identifier)
                {
                    if (aa.Responses.Count > 0)
                    {
                        AssessmentResponse ar = aa.Responses[0];
                        ar.RefId = hidResponseText.Value;
                        return aa.Responses[0];
                    }
                }
            }
            else
            {
                foreach (AssessmentResponse rsp in aa.Responses)
                {
                    if (hidResponseID.Value == rsp.Pattern)
                    {
                        AssessmentResponse ar = rsp;
                        ar.RefId = hidResponseText.Value;
                        return ar;
                    }
                }
            }
        }
        AssessmentResponse war = new AssessmentResponse();
        war.Score = -1;
        war.RefId = hidResponseText.Value;
        return war;
    }

    void cbl_SelectedIndexChanged(object sender, EventArgs e)
    {
        hidResponseID.Value = "";
        hidResponseText.Value = "";

        CheckBoxList cbl = (CheckBoxList)sender;
        foreach (ListItem li in cbl.Items)
        {
            if (li.Selected)
            {
                hidResponseID.Value += li.Value + "||";
                hidResponseText.Value += li.Text + "||";
            }
        }

        if (hidResponseID.Value != "")
            hidResponseID.Value = hidResponseID.Value.Remove(hidResponseID.Value.Length - 2, 2);
        if (hidResponseText.Value != "")
            hidResponseText.Value = hidResponseText.Value.Remove(hidResponseText.Value.Length - 2, 2);
    }

    void rbl_SelectedIndexChanged(object sender, EventArgs e)
    {
        hidResponseID.Value = ((RadioButtonList)sender).SelectedValue;
        hidResponseText.Value = ((RadioButtonList)sender).SelectedItem.Text;
    }

    void tb_TextChanged(object sender, EventArgs e)
    {
        hidResponseText.Value = ((TextBox)sender).Text;
    }
}
